package MonotonicStack2;

import org.junit.jupiter.api.Test;

import java.util.Stack;

/**
 * @author Lzm
 * @version 1.0
 */
public class dailyTemperatures_ {

  @Test
  public void dailyTemperaturesTest(){
    int[] temperatures = new int[]{73,74,75,71,69,72,76,73};
    int[] ints = dailyTemperatures(temperatures);
    for (int anInt : ints) {
      System.out.println(anInt);
    }
  }

  public int[] dailyTemperatures(int[] temperatures) {
    if(temperatures.length == 1){
      return new int[]{0};
    }

    Stack<Integer> stack = new Stack<>();
    stack.push(0);
    int[] result = new int[temperatures.length];
    result[result.length - 1] = 0;

    for (int i = 0; i < temperatures.length; i++) {
      while(!stack.isEmpty() && temperatures[i] > temperatures[stack.peek()]){
        result[stack.peek()] = i - stack.peek();
        stack.pop();
      }
      stack.push(i);
    }
    return result;
  }
}
